/*******************************************************************************
 *
 * THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * 
 ******************************************************************************/
using System;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Microsoft.Samples
{
    class MetadataWalker
    {
        [STAThread]
        public static void Main( string[] args )
        {
            Console.WriteLine ( "WPF Imaging Metadata Walker" );
            Console.WriteLine ( "-------------------------------------------------------------------------------" );

            if ( args.Length != 1 )
            {
                Console.WriteLine ( "Usage: MetadataWalker <filename>" );
                return;
            }

            using ( Stream fileStream = File.Open ( args[0], FileMode.Open ) )
            {
                BitmapDecoder decoder = BitmapDecoder.Create ( fileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None );

                Console.WriteLine ( "Full Query, Relative Query, Value" );

                PrintMetadata ( decoder.Frames[0].Metadata, string.Empty );
            }
        }

        private static void PrintMetadata( ImageMetadata metadata, string fullQuery )
        {
            BitmapMetadata theMetadata = metadata as BitmapMetadata;

            if ( theMetadata != null )
            {
                foreach ( string query in theMetadata )
                {
                    string tempQuery = fullQuery + query;

                    // query string here is relative to the previous metadata reader.
                    object o = theMetadata.GetQuery ( query );

                    Console.WriteLine ( "{0}, {1}, {2}", tempQuery, query, o );

                    BitmapMetadata moreMetadata = o as BitmapMetadata;

                    if ( moreMetadata != null )
                    {
                        PrintMetadata ( moreMetadata, tempQuery );
                    }
                }
            }
        }
    }
}
